//
//  SCCarNumTipLabel.m
//  Shuaiche
//
//  Created by zzs on 2018/7/12.
//  Copyright © 2018年 zzs. All rights reserved.
//

#import "SCCarNumTipLabel.h"
#import "NSString+Helper.h"

static const CGFloat height = 30.f;
static const CGFloat inset = 12.f;
@interface SCCarNumTipLabel()
@property (nonatomic, assign) BOOL isAnimation;
@end

@implementation SCCarNumTipLabel

+ (instancetype)intitWithTitle:(NSString *)title font:(UIFont*)font maxY:(CGFloat)maxY;{
    SCCarNumTipLabel *label = [[SCCarNumTipLabel alloc] init];
    label.autoresizingMask = UIViewAutoresizingNone;
    label.font = font;
    CGSize size = [NSString getSizeWithString:title Font:font maxSize:CGSizeMake(CGFLOAT_MAX, height)];
    label.text = title;
    label.textAlignment = 1;
    label.frame = CGRectMake(0, 0, size.width+height, height);
    CAGradientLayer *gradient = (CAGradientLayer *)label.layer;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)SCColor(0xffde00).CGColor,
                       (id)SCColor(0xffba00).CGColor,
                       nil];
    gradient.startPoint = (CGPoint){0,1};
    gradient.endPoint =  (CGPoint){1,1};
    gradient.cornerRadius = height/2;
    label.center = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds)/2., maxY - height/2);
    return label;
}

// ios 13 再添加一个渐变层会遮挡原有的layer
+ (Class)layerClass {
    return CAGradientLayer.class;
}

- (void)show{
    if(self.isAnimation)return;
    self.isAnimation = YES;
    WEAKSELF
    [UIView animateWithDuration:0.7 animations:^{
        [self setCenter:CGPointMake(self.center.x, self.center.y + height+inset)];
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),dispatch_get_main_queue(), ^{
            [weakSelf disAppear];
        });
    }];
}

- (void)disAppear{
    [UIView animateWithDuration:0.3 animations:^{
         [self setCenter:CGPointMake(self.center.x, self.center.y - height-inset)];
    } completion:^(BOOL finished) {
        self.isAnimation = NO;
        [self removeFromSuperview];
    }];
}

@end
